package cn.ly.rabbit.rabbitListener;

import cn.ly.rabbit.config.RabbitConfig;
import cn.ly.rabbit.model.Order;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * 死信消息：情况二
 * 生产者将消息设置了过期时间，投递到目标队列后，无人消费：故消息过期成为了死信消息    //该目标队列设置了死信交换器和死信路由
 * 则将会转发到死信队列中
 *
 * 使用场景：【延时订单】
 */
@Component
@RabbitListener(queues = RabbitConfig.Delay_Queue_Name)   //监听死信队列2
public class DelayOrderQueueListener {

    @RabbitHandler
    public void process( String msg, Channel channel, Message message)  {
        try {
            //拿到消息
//            String mm = new String(message.getBody());
            System.out.println("监听到myDelay_Queue消息：>>>>>>>>>>>>>>msg:"+msg);
            //...模拟业务逻辑
            if(msg.contains("0")){
                System.out.println(">>>>>>>>>>>>>>您的订单超过10s，仍为支付，我们将自动取消该订单信息：开始操作DB...");
            }else{
                System.out.println(">>>>>>>>>>>订单已支付，over");
            }

            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);  //第二个参数表示批量确认
        } catch (Exception e) {
            System.out.println(">>>>>>>>>>>>>>>拒绝任务");
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);   //第二个参数表示是否重新返回队列：requeue
            } catch (IOException e1) {
                System.out.println(">>>>>>>>>>>>>>>拒绝报错");
            }
        }
    }


}
